package regpang.myapplication22.viewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import regpang.myapplication22.R;


public class ViewPagerAniMainActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private Button mBtnGo;
    private ViewPaperAdapter vpAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pager_main);
        initViews();
        mViewPager.setCurrentItem(1);
    }

    private void initViews() {


        mViewPager = (ViewPager) findViewById(R.id.vp_guide);
        mBtnGo = (Button) findViewById(R.id.btn_go);

        //实例化各个界面的布局对象
        View view1 = View.inflate(this, R.layout.guide_view, null);
        View view2 = View.inflate(this, R.layout.guide_view, null);
        View view3 = View.inflate(this, R.layout.guide_view, null);
        View view0 = View.inflate(this, R.layout.guide_view, null);
        View view4 = View.inflate(this, R.layout.guide_view, null);

        ((ImageView) view1.findViewById(R.id.tv_pic)).setImageResource(R.drawable.android_guide_step_1);
        ((ImageView) view2.findViewById(R.id.tv_pic)).setImageResource(R.drawable.android_guide_step_2);
        ((ImageView) view3.findViewById(R.id.tv_pic)).setImageResource(R.drawable.android_guide_step_3);
        ((ImageView) view0.findViewById(R.id.tv_pic)).setImageResource(R.drawable.android_guide_step_3);
        ((ImageView) view4.findViewById(R.id.tv_pic)).setImageResource(R.drawable.android_guide_step_3);

        mBtnGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ViewPagerAniMainActivity.this, "Go Home", Toast.LENGTH_SHORT).show();
            }
        });

        view1.findViewById(R.id.tv_pic).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(1);
            }
        });
        view2.findViewById(R.id.tv_pic).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(2);
            }
        });
        view3.findViewById(R.id.tv_pic).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(3);
            }
        });


        ((TextView) view4.findViewById(R.id.tv_title)).setText("IT蓝豹首页");
        ((TextView) view4.findViewById(R.id.tv_title)).setTextColor(Color.parseColor("#FF5000"));
        ((TextView) view1.findViewById(R.id.tv_title)).setText("IT蓝豹首页");
        ((TextView) view1.findViewById(R.id.tv_title)).setTextColor(Color.parseColor("#FF5000"));
        ((TextView) view2.findViewById(R.id.tv_title)).setText("android 特效");
        ((TextView) view2.findViewById(R.id.tv_title)).setTextColor(Color.parseColor("#49ca65"));
        ((TextView) view3.findViewById(R.id.tv_title)).setText("一键预览");
        ((TextView) view3.findViewById(R.id.tv_title)).setTextColor(Color.parseColor("#16c5c6"));
        ((TextView) view0.findViewById(R.id.tv_title)).setText("一键预览");
        ((TextView) view0.findViewById(R.id.tv_title)).setTextColor(Color.parseColor("#16c5c6"));

        ((TextView) view1.findViewById(R.id.tv_desc)).setText("IT蓝豹（www.itlanbao.com）\n");
        ((TextView) view2.findViewById(R.id.tv_desc)).setText("一个专业特效分享网站\n");
        ((TextView) view3.findViewById(R.id.tv_desc)).setText("欢迎学习代码上IT蓝豹\n");

        mViewPager = (ViewPager) findViewById(R.id.vp_guide);

        ArrayList<View> views = new ArrayList<>();
        views.add(view4);
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view0);

        vpAdapter = new ViewPaperAdapter(views);

        mViewPager.setOffscreenPageLimit(views.size());
        mViewPager.setPageMargin(-dip2px(135));
        mViewPager.setAdapter(vpAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                //循环视图
                if (position == mViewPager.getAdapter().getCount()-1){
                    mViewPager.setCurrentItem(1,false);
                }
                if (position == 0){
                    mViewPager.setCurrentItem(mViewPager.getAdapter().getCount()-2,false);
                }

                //不循环视图
//                if (position + 1 == mViewPager.getAdapter().getCount()) {
//                    if (mBtnGo.getVisibility() != View.VISIBLE) {
//                        mBtnGo.setVisibility(View.VISIBLE);
//                        mBtnGo.startAnimation(AnimationUtils.loadAnimation(ViewPagerAniMainActivity.this, android.R.anim.fade_in));
//                    }
//                } else {
//                    if (mBtnGo.getVisibility() != View.GONE) {
//                        mBtnGo.setVisibility(View.GONE);
//                        mBtnGo.startAnimation(AnimationUtils.loadAnimation(ViewPagerAniMainActivity.this, android.R.anim.fade_out));
//                    }
//                }
            }
        });


//        mViewPager.setPageTransformer(false,new RotateDownPageTransformer());
        int flag = getIntent().getIntExtra("flag", 0);
        if (flag == 0) {
            mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        } else if (flag == 1) {
            mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        } else if (flag == 2) {
            mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        }


    }


    private int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
